/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.baseConfig.service.impl;

import com.koron.workflow.baseConfig.bean.DepartmentBean;
import com.koron.workflow.baseConfig.mapper.DepartmentMapper;
import com.koron.workflow.baseConfig.service.DepartmentService;
import com.koron.workflow.common.bean.PageBean;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.common.util.DisposeBeanUtil;
import com.koron.workflow.common.util.TreeUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * @author qingfeng
 * @description: TODO
 * @date 2020/4/13 11:04
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Override
    @TaskAnnotation("queryTree")
    public List<DepartmentBean> queryTree(SessionFactory factory) {
        DepartmentMapper mapper = factory.getMapper(DepartmentMapper.class);
        List<DepartmentBean> list = mapper.queryForList(null,null,null);
        return TreeUtils.list2TreeList(list,"code","parentCode","children");
    }

    @Override
    @TaskAnnotation("queryForAll")
    public List<DepartmentBean> queryForAll(SessionFactory factory) {
        DepartmentMapper mapper = factory.getMapper(DepartmentMapper.class);
        List<DepartmentBean> list = mapper.queryForList(null,null,null);
        return list;
    }

    @Override
    @TaskAnnotation("queryForList")
    public PageBean<DepartmentBean> queryForList(SessionFactory factory,String parentCode, Integer page, Integer pageSize) {
        DepartmentMapper mapper = factory.getMapper(DepartmentMapper.class);
        List<DepartmentBean> list = mapper.queryForList(parentCode,page,pageSize);
        int total = mapper.countForList(parentCode);
        PageBean<DepartmentBean> result = new PageBean<>(list,total);
        return result;
    }

    @Override
    @TaskAnnotation("save")
    public MessageBean<String> save(SessionFactory factory, DepartmentBean role, StaffBean user) {
        MessageBean<String> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(),"保存成功",String.class);
        DepartmentMapper mapper = factory.getMapper(DepartmentMapper.class);
        DepartmentBean bean = mapper.queryForCode(role.getCode());
        if (StringUtils.isEmpty(role.getId())) {
            DisposeBeanUtil.disposeInsert(user,role);
            if (bean != null) {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("编码已存在");
                return msg;
            }
            mapper.insert(role);
        } else {
            DisposeBeanUtil.disposeUpdate(user,role);
            if (bean != null && !bean.getId().equals(role.getId())) {
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("编码已存在");
                return msg;
            }
            mapper.update(role);
        }
        return msg;
    }

    @Override
    @TaskAnnotation("delete")
    public void delete(SessionFactory factory, String id) {
        DepartmentMapper mapper = factory.getMapper(DepartmentMapper.class);
        mapper.delete(id);
    }

    @Override
    @TaskAnnotation("queryForObject")
    public DepartmentBean queryForObject(SessionFactory factory, String id) {
        DepartmentMapper mapper = factory.getMapper(DepartmentMapper.class);
        return mapper.queryForObject(id);
    }
}
